<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 组织架构-头部 -->
      <el-card>
        <!-- 放置结构内容 -->
        <treeTools :tree-node="company" :is-root="true" @openAddDeptWindow="openAddDeptWindow" />
        <el-tree :data="departs" :props="defaultProps" :default-expand-all="true">
          <!-- 给插槽传入内容，会循环多次，有多少节点就会循环多少次 -->
          <!-- 作用域插槽 slot-scope='obj' 接收传递给插槽的数据 data就是每个节点的数据对象-->
          <treeTools slot-scope="{data}" :tree-node="data" @delDepts="delDepts" @openAddDeptWindow="openAddDeptWindow" @openEditDeptWindow="openEditDeptWindow" />
        </el-tree>
      </el-card>
    </div>
    <addDepts ref="addDept" :show-dialog.sync="showDialog" :tree-node="node" @addDepts="getDepartments" />
  </div>
</template>

<script>
import treeTools from './components/tree-tools.vue'
import addDepts from './components/add-dept.vue'
import { getDepartments } from '@/api/departments'
import { tranListToTree } from '@/utils/'
export default {
  name: 'Department',
  components: {
    treeTools,
    addDepts

  },
  data() {
    return {
      company: { name: '', manager: '负责人', id: '' },
      departs: [],
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      showDialog: false,
      node: null// 记录当前点击的Node节点

    }
  }, created() {
    this.getDepartments()
  },
  methods: {
    async getDepartments() {
      const res = await getDepartments()
      console.log(res)
      this.company.name = res.companyName
      this.departs = tranListToTree(res.depts, '')// 需要转换为树形结构才可以使用
      // this.departs = res.depts
    },
    /** 删除部门后重新获取部门信息 */
    delDepts() {
      this.getDepartments()
    },
    /** treeNode组件要求打开添加子部门窗口，并传入了点击的部门 */
    openAddDeptWindow(treeNode) {
      this.showDialog = true// 显示弹层
      // 当前点击的部门，应该记录下来
      this.node = treeNode
    },
    /** treeNode组件要求打开编辑部门窗口，并传入了点击的部门 */
    openEditDeptWindow(treeNode) {
      this.showDialog = true// 显示弹层
      // 当前点击的部门，应该记录下来
      this.node = treeNode
      // 应该在这里调用获取部门详情的方法
      this.$refs.addDept.getDeptDetail(treeNode.id)
    }
  }
}
</script>

<style>
.el-dropdown-link {
    cursor: pointer;
    color: #409EFF;
  }
  .el-icon-arrow-down {
    font-size: 12px;
  }
</style>

